//
//  ProViewController.m
//  09-Homework
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ProViewController.h"
#import "AreaViewController.h"

@interface ProViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *proArr;
@property (nonatomic, strong)NSMutableArray *cityArr;
@property (nonatomic, strong)NSMutableArray *areaArr;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation ProViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"省";
        [self creatNSString];
    [self creatTableView];
}
- (void)creatNSString {
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-Homework/09-Homework/area.txt";
    self.proArr = [NSMutableArray array];
    self.cityArr = [NSMutableArray array];
    NSString *areaString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [areaString componentsSeparatedByString:@"\n"];
    
    for (NSString *area in strArr) {
        if (![area hasPrefix:@" "]) {
            Pro *pro = [Pro zoneWithName:area];
            [_proArr addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Pro *pro = [_proArr lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[_proArr lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
            
        }
    }
    NSLog(@"%@", _proArr);
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [_proArr[section] name];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _proArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_proArr[section] array].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    City *city = [_proArr[indexPath.section] array][indexPath.row];
    cell.textLabel.text = city.name;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AreaViewController *area = [[AreaViewController alloc] init];
    area.arrArea = [NSMutableArray arrayWithArray:[_proArr[indexPath.section] array]];
    
    [self.navigationController pushViewController:area animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
